package com.syzg.core.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClassTypeUtil
{
  public static final int INTEGER = 0;
  public static final int FLOAT = 1;
  public static final int DOUBLE = 2;
  public static final int BIGDECIMAL = 3;
  public static final int BOOLEAN = 4;
  public static final int STRING = 5;
  public static final int DATE_SQL = 6;
  public static final int TIMESTAMP = 7;
  public static final int SYBASETIME = 8;
  public static final int LONG = 9;
  public static final int DATE_UTIL = 10;
  public static final int Byte = 11;
  public static final int COLLECTION = 12;
  public static final int LIST = 13;
  public static final int MAP = 14;
  public static final int SET = 15;
  private static final HashMap<String, String> map = new HashMap();

  static { map.put("java.lang.Integer", String.valueOf(0));
    map.put("java.lang.Float", String.valueOf(1));
    map.put("java.lang.Double", String.valueOf(2));
    map.put("java.math.BigDecimal", String.valueOf(3));
    map.put("java.lang.Boolean", String.valueOf(4));
    map.put("java.lang.String", String.valueOf(5));
    map.put("java.sql.Date", String.valueOf(6));
    map.put("java.util.Date", String.valueOf(10));
    map.put("java.sql.Timestamp", String.valueOf(7));
    map.put("com.sybase.jdbc2.tds.SybTimestamp", String.valueOf(8));
    map.put("java.lang.Long", String.valueOf(9));
    map.put("java.lang.Byte", String.valueOf(11));
    map.put("java.util.Collection", String.valueOf(12));
    map.put("java.util.List", String.valueOf(13));
    map.put("java.util.Map", String.valueOf(14));
    map.put("java.util.Set", String.valueOf(15));
  }

  public static int getTypeByClass(Class<?> _cls)
  {
    return getTypeByClassName(_cls.getName());
  }

  public static int getTypeByClass(String _clsName) {
    return getTypeByClassName(_clsName);
  }

  public static int getTypeByClassName(String _clsName) {
    String riv = (String)map.get(_clsName);
    if (riv == null) {
      return -1;
    }
    return Integer.valueOf(riv).intValue();
  }

  public static boolean isNumberType(Class<?> _cls)
  {
    return isNumberType(_cls.getName());
  }

  public static boolean isNumberType(String _clsName) {
    switch (getTypeByClass(_clsName)) {
    case 0:
      return true;
    case 1:
      return true;
    case 2:
      return true;
    case 3:
      return true;
    case 9:
      return true;
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    }

    return false;
  }

  private static boolean checkType(Class<?> cls, Class<?> objCls)
  {
    if (cls.getName().equals(objCls.getName())) {
      return true;
    }
    Class[] ifCls = objCls.getInterfaces();
    if ((ifCls != null) && (ifCls.length > 0))
    {
      Class[] aclass;
      int j = (aclass = ifCls).length;
      for (int i = 0; i < j; i++)
      {
        Class icls = aclass[i];
        if (icls.getName().equals(cls.getName())) {
          return true;
        }
        if (icls.getSuperclass() != null) {
          objCls = objCls.getSuperclass();
          boolean tempBl = checkType(cls, objCls);
          if (tempBl) {
            return true;
          }
        }
      }
    }

    if (objCls.getSuperclass() != null) {
      objCls = objCls.getSuperclass();
      return checkType(cls, objCls);
    }
    return false;
  }

  public static boolean isList(Class<?> cls)
  {
    return checkType(List.class, cls);
  }

  public static boolean isCollection(Class<?> cls) {
    return checkType(Collection.class, cls);
  }

  public static boolean isMap(Class<?> cls) {
    return checkType(Map.class, cls);
  }

  public static boolean isSet(Class<?> cls)
  {
    return checkType(Set.class, cls);
  }
}